using System;
using System.Collections.Specialized;
using ZP.Data;
using Kwan.Data;

namespace ZP.Service
{
    public sealed class JobQuery
	{
        public static Expression MakeSearchWhere(NameValueCollection ps)
		{
            Expression exp = new Expression();

            string propName = "EntityID";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.entityID == ps[propName];

            propName = "CompanyID";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.companyID == ps[propName];
            propName = "ContactPerson";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.contactPerson == ps[propName];

            //if (!string.IsNullOrEmpty(ps["Click"]))
            //    exp &= VJob.click == ps["Click"];
            propName = "Degreefrom";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.degreefrom == ps[propName];
            propName = "Description";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.description == ps[propName];
            propName = "Duty";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.duty == ps[propName];
            propName = "EndDate";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.endDate < DateTime.Parse(ps[propName]);
            propName = "Enrollments";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.enrollments > ps[propName];
            propName = "Funtype";
            if (!string.IsNullOrEmpty(ps[propName]) && ps[propName] != "0000")
            {
                bool isParent = ps[propName].Substring(2, 2) == "00";
                if (isParent)
                {
                    exp &= VJob.funtype.Like(ps[propName].Substring(0, 2) + "%");
                }
                else
                    exp &= VJob.funtype == ps[propName];
            }
            propName = "IndustryCode";
            if (!string.IsNullOrEmpty(ps[propName]) && ps[propName] != "00")
            {
                bool isParent = ps[propName].Substring(2, 2) == "00";
                if (isParent)
                {
                    exp &= VJob.industryCode.Like(ps[propName].Substring(0, 2) + "%");
                }
                else
                    exp &= VJob.industryCode == ps[propName];
            }
            propName = "Issuedate";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.issuedate == ps[propName];
            propName = "Jobarea";
            if (!string.IsNullOrEmpty(ps[propName]) && ps[propName] != "0000")
            {
                bool isParent = ps[propName].Substring(2, 2) == "00";
                if (isParent)
                {
                    exp &= VJob.jobarea.Like(ps[propName].Substring(0, 2) + "%");
                }
                else
                    exp &= VJob.jobarea == ps[propName];
            }
            propName = "JobName";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.jobName == ps[propName];
            propName = "Jobterm";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.jobterm == ps[propName];
            propName = "Match";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.match == ps[propName];
            propName = "Providesalary";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.providesalary == ps[propName];
            propName = "Scale";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.scale == ps[propName];
            propName = "Sex";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.sex == ps[propName];
            propName = "Workyear";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= VJob.workyear == ps[propName];

            return exp;
		}
	}
}